/* @(#)GSProviderUploadAction.java    V0.1
 *
 * Project: GSdevelop
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   owen         2006-2-23
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.io.DataInputStream;
import java.net.URL;

import org.apache.struts.upload.FormFile;

import com.hitrust.gs.business.GSFileUploadProcessor;
import com.hitrust.gs.form.GSProviderUploadForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;

/**
 * 导入供应商信息
 */
public class GSProviderUploadAction  extends GSBaseAction{
    public String execute(GSBaseForm aForm) throws Exception {
        GSProviderUploadForm tForm = (GSProviderUploadForm)aForm;
        FormFile tProviderFile = tForm.getTheFile();
        String tResult="";
        //UserBean tUserBean = (UserBean)aForm.getSession().getAttribute(BaseConstants.LOGIN);
        
        DataInputStream dis = null;
        
        String tDay=DateUtil.getNextUpDay(-1);
        //文件下载
        String Name="/" + GSSystemConfigurator.getParam("gs.purcode") + "_Company_"+tDay+".xls"; 
        
        URL url = new URL(GSSystemConfigurator.getParam("gs.down.schema")+"://"+GSSystemConfigurator.getParam("gs.down.host")+":"+GSSystemConfigurator.getParam("gs.down.port")+"/"+GSSystemConfigurator.getParam("gs.down.fullpath")+Name);
        
        dis = new DataInputStream(url.openStream());
        
        try{
            //导入商户信息
            tResult=GSFileUploadProcessor.batchImportProvider(dis,"");
        }catch(GSException e){
                aForm.getRequest().setAttribute("exception_desc",e.getMessage());
                //aForm.getRequest().setAttribute("exception_code",e.getCode());
                aForm.getRequest().setAttribute("action","A401001");
                throw new GSException(e.getMessage());
        }
        //从返回的值中取得失败和成功的数据
        String tSuccess= tResult.split("-")[0];
        String tFail= tResult.split("-")[1];
        String tOutputPath = tResult.split("-")[2];
        aForm.getRequest().setAttribute("success",tSuccess);
        aForm.getRequest().setAttribute("fail",tFail);
        if(Integer.parseInt(tFail)>0){
            aForm.getRequest().setAttribute("faildown",tOutputPath);
            if("0".equals(tOutputPath))
                aForm.getRequest().setAttribute("faildownfailure","");
        }
        return GSBaseConstants.FORWARD_SUCCESS;
    }
}
